<script type ="text/javascript" src="date/jquery.jdpicker.js"></script>
<link rel="stylesheet" href="date/jdpicker.css" type="text/css" media="screen" /> 
<?php
	// Mã chủ đề
	$MaCM = $_GET['mcm'];
	$sql="select * from chuyenmuc where MaChuyenMuc=$MaCM";
	$dschuyenmuc = mysql_query($sql);
	$tenchuyenmuc=mysql_fetch_array($dschuyenmuc);

?>
<div id="ChuyenMucBaiViet">&nbsp<?php echo "<td class='hienthi'>Bạn đang ở chuyên mục: {$tenchuyenmuc['TenChuyenMuc']} </td>"; ?></div>
<?php 
if($_GET['page']) {
$page = $_GET['page'];
$current_page = $page;
$page -= 1;
$display = 5;
$start = $page * $display;
$data = "";

$sql_baiviet_chuyenmuc = "SELECT *
							  FROM baiviet B, loaichuyenmuc C
							  WHERE B.MaLoaiChuyenMuc = C.MaLoaiChuyenMuc AND C.MaChuyenMuc = $MaCM and B.XuatBan=1
							  LIMIT $start, $display";
$danhsach_baiviet_chuyenmuc = mysql_query($sql_baiviet_chuyenmuc);
?>

<table class="NoBorder" width="700px">
	<?php
		// Lấy danh sách bài viết thuộc chủ đề
		while($dong_baiviet_chuyenmuc = mysql_fetch_array($danhsach_baiviet_chuyenmuc))
		{
			echo "<tr>";
				echo "<td colspan='2' class='TieuDe'><a href='news.php?do=baivietchitiet&mbv={$dong_baiviet_chuyenmuc['MaBaiViet']}'><h4>{$dong_baiviet_chuyenmuc['TieuDe']}</h4></a></td>";
			echo "</tr>";	
			echo "<tr>";
				echo "<td colspan='2' class='NgayDang'>Ngày đăng: {$dong_baiviet_chuyenmuc['NgayDang']}</td>";
			echo "</tr>";
            $HinhAnhPath = substr($dong_baiviet_chuyenmuc["HinhAnh"], 3);
            echo "<tr>";
                echo "<td class='HinhAnh'><img width='100' height='100' src='{$HinhAnhPath}' /></td>";
				echo "<td align='justify' class='TomTat'>" . ($dong_baiviet_chuyenmuc['TomTat']) . "</td>";
			echo "</tr>";
			echo "<tr>";
			echo "<td></td>";
			echo "<td class='btn_more'><a href='news.php?do=baivietchitiet&MCM".$MaCM."&mbv={$dong_baiviet_chuyenmuc['MaBaiViet']}'> xem tiếp... </a></td>";
			echo "</tr>";
		}
	?>
</table>

<?php 
$data = "<nav role='list'>
			<ul>".$data."</ul>
		</nav>";
$query_page = "SELECT COUNT(*) FROM baiviet B, loaichuyenmuc C
							  WHERE B.MaLoaiChuyenMuc = C.MaLoaiChuyenMuc AND C.MaChuyenMuc = $MaCM and B.XuatBan=1";
$result_page = mysql_query($query_page);
$row = mysql_fetch_array($result_page);
$count = $row['COUNT(*)'];
$pages = ceil($count / $display);

if ($current_page >= 7) 
{
	$start_page = $current_page - 3;
	if ($pages > $current_page + 3)
		$end_page = $current_page + 3;
	else if ($current_page <= $pages && $current_page > $pages - 6) 
	{
			$start_page = $pages - 6;
			$end_page = $pages;
	} 
	else 
	{
		$end_page = $pages;
	}
} 
else 
{
	$start_page = 1;
	if ($pages > 7)
		$end_page = 7;
	else
		$end_page = $pages;
}

$data .= "";
?>
<div style="text-align:center;">
<?php
if ($current_page > 1) 
{
	$data .= "<a href='news.php?do=baivietchuyenmuc&mcm={$MaCM}&page=1'>Đầu tiên</a>&nbsp";
	$previous = $current_page - 1;
	$data .= "<a href='news.php?do=baivietchuyenmuc&mcm={$MaCM}&page={$previous}'>Trước</a>&nbsp";
} 

for ($i = $start_page; $i <= $end_page; $i++) 
{
	if ($current_page == $i)
		$data .= "<b>{$i}</b>&nbsp";
	else
		$data .= "<a href='news.php?do=baivietchuyenmuc&mcm={$MaCM}&page={$i}'>{$i}</a>&nbsp";
}

if ($current_page < $pages) 
{
	$next = $current_page + 1;
	$data .= "<a href='news.php?do=baivietchuyenmuc&mcm={$MaCM}&page={$next}'>Tiếp</a>&nbsp";
	$data .= "<a href='news.php?do=baivietchuyenmuc&mcm={$MaCM}&page={$pages}'>Cuối</a>&nbsp";
} 

$total_page = "<span class='per_page'>Trang ". $current_page ." => $pages </span>";



echo $data;

}
?>

	<form id="xemtheongay" method="post" action="news.php?do=xembaitheongay&mcm=<?php echo $MaCM;?>&page=1" onsubmit="return checkPostArtForm(this);">
        <p style="font-family: Arial">
            <input style="text-align: center;" class="jdpicker" type="text" name="date" id="date" size="30" />
            <br/>
            <a class="button small blue" href="javascript:{}" onclick="document.getElementById('xemtheongay').submit(); return false;">Xem bài theo ngày</a>
        </p>
        <a style="font-family: Arial; font-size: 10pt;" href="news.php?do=baivietngaytruoc&mcm=<?php echo $MaCM;?>&page=1">Bài viết ngày trước</a>
    </form>
</div>
<br />
<script type="text/javascript">
	$('.jdpicker').jdPicker({date_format: 'dd/mm/YYYY'});
    function checkPostArtForm(frm)
    {
    	if(frm.date.value == "")
    	{
    		alert('Hãy chọn ngày cần xem!');
    		return false;
    	}
    	return false;
    }
</script>



